package com.yiren.dbaa.module.practice_set.carmodel.presenter;

import android.app.Activity;

import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.PointBean;
import com.yiren.backstage.currency.bean.PointExBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelCollectContract;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelCollectModel;
import com.yiren.dbaa.module.practice_set.carmodel.view.CarMapView;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/12/7
 */
public class CarModelCollectPresenter implements CarModelCollectContract.Presenter {

    private Activity activity;
    private CarModelCollectContract.Model carModelCollectModel;

    public CarModelCollectPresenter(Activity activity) {
        this.activity = activity;
        carModelCollectModel = new CarModelCollectModel(activity);
    }

    @Override
    public boolean isCanCollectABCD(List<PointExBean> carModelPoints) {
        PointExBean zeroPointEx = carModelPoints.get(0);
        if (null == zeroPointEx) {
            return true;
        } else {
            if (zeroPointEx.getX() != 0 && zeroPointEx.getY() != 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void initParameter(List<PointExBean> carModelPoints, CarModelCollectContract.View view) {
        if (null != carModelPoints && carModelPoints.size() > 0) {
            PointExBean zeroPointEx = carModelPoints.get(0);
            if (null != zeroPointEx && zeroPointEx.getX() == 0 && zeroPointEx.getY() == 0) {
                view.showZeroPointDialog();
            }
        }
        MessageBox.show(activity, "当前车辆模型未保存");
    }

    @Override
    public boolean isCanRecover(List<PointExBean> carModelPoints) {
        if (null != carModelPoints && carModelPoints.size() > 0) {
            PointExBean zeroPointEx = carModelPoints.get(0);
            if (null != zeroPointEx && zeroPointEx.getX() == 0 && zeroPointEx.getY() == 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isCanFix(PointExBean zeroPoint) {
        if (zeroPoint.getX() != 0 && zeroPoint.getY() != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isPointCollect(List<PointExBean> carModelPoints) {
        for (int i = 1; i < carModelPoints.size(); i++) {
            if (null == carModelPoints.get(i)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void recoverCarModelPoint(List<PointExBean> carModelPoints, GpsInfoBean gps) {
        if (null != carModelPoints && carModelPoints.size() > 0) {
            for (int i = 0; i < carModelPoints.size(); i++) {
                PointExBean pointEx = carModelPoints.get(i);
                pointEx.angle += gps.getAzimuth();
                pointEx.fixXY();
                pointEx.addXY(new PointBean(gps.getDistanceX(), gps.getDistanceY()));
            }
        }
    }

    @Override
    public void recoverPointABCD(PointExBean point, int pointIndex, GpsInfoBean gps, CarMapView carMapView) {
        if (null != point) {
            point.angle += gps.getAzimuth();
            point.fixXY();
            point.addXY(new PointBean(gps.getDistanceX(), gps.getDistanceY()));
            carMapView.setFramePoint(pointIndex, point);
        }
    }

    @Override
    public void fixPointABCD(PointExBean originPoint, PointExBean point, int pointIndex, GpsInfoBean gps, CarMapView carMapView) {
        if (null != point) {
            point.setPoint(point.getX() - originPoint.getX(), point.getY() - originPoint.getY());
            point.setAngle((float) (point.getAngle() - gps.getAzimuth()));
            point.fixXY();
            carMapView.setFramePoint(pointIndex, point);
        }
    }

    @Override
    public void fixCarModelPoint(List<PointExBean> carModelPoints, float radius, GpsInfoBean gps, CarModelEntity carModel) {
        carModelCollectModel.fixOtherModelPoint(carModelPoints, gps);
        //将0点修改成原点（0，0）
        PointExBean zeroPointEx = carModelPoints.get(0);
        if (null != zeroPointEx) {
            zeroPointEx.setPoint(0, 0);
            if (radius != 0) {
                carModelCollectModel.fixPointByRadius(carModelPoints, radius, new CommonCallBack() {
                    @Override
                    public void complete() {
                        carModelCollectModel.saveCarModel(carModel, carModelPoints);
                    }
                });
            } else {
                carModelCollectModel.saveCarModel(carModel, carModelPoints);
            }
        }
    }

}